myApp.controller('asset_operation', function ($scope, $http, $uibModalInstance, formBoxData, httpService, $rootScope) {

	/*关闭的图片*/
	$scope.closeImg={'img':'images/close-default.png','defaultImg':'images/close-default.png','downImg':'images/close-btndown.png','hoverImg':'images/close-hover.png'};
    $scope.title = formBoxData.title;
    $scope.useStatusFlag = formBoxData.useStatusFlag;
    $scope.checkNum=$rootScope.select_list_index.length;
    $scope.formData={
        userInfo:{
            userId:'kjal535sjdfl43ssfs123123df',
            name:'张三',
            email:'4356565@qq.com',
            phone:'1856563232'
        },
        operationData:{
            useStatus:'',
            dealStatus:''
        }
    }

	// 获取人员列表
    $scope.getPeopleList = function () {
        $http({
            method: 'GET',
            url: 'json/peopleList.json'
        }).success(function (response) {
            $scope.user = undefined;
            $scope.peopleList = response.data;
        }).error(function (response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    };
    $scope.setFormUser = function () {
        $scope.formData.userId = $scope.user.id;
        console.log($scope.formData);
    };

    // 获取管理员列表
    $scope.getManagerList = function () {
        $http({
            method: 'GET',
            url: 'json/managerList.json'
        }).success(function (response) {
            $scope.manager = undefined;
            $scope.managerList = response.data;
        }).error(function (response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    };
    $scope.setFormManager = function () {
        $scope.formData.manager = $scope.manager.id;
        console.log($scope.formData);
    };

    // 获取资产类型列表
    $scope.getAssetTypeList = function () {
        $http({
            method: 'GET',
            url: 'json/assetTypeList.json'
        }).success(function (response) {
            $scope.assetTypeList = response.data;
            $scope.formData.assetType = undefined;
        }).error(function (response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    };

    function formInit() {
        $scope.getPeopleList();
        $scope.getManagerList();
        $scope.getAssetTypeList();
    }
    formInit();

    // 日期选择
    $scope.datePopup = {
        opened: false
    };
    $scope.dateOpen = function () {
        $scope.datePopup.opened = true;
    };


    $scope.changeUseStatus=function(checkedIds,formData){
        console.log('changeUseStatus');
        var assets={
            useStatus:formData.operationData.useStatus,
            userId:formData.userInfo.userId,
            getTime:formData.operationData.getTime||null,
            borrowTime:formData.operationData.borrowTime||null,
            predictReturnTime:formData.operationData.predictReturnTime||null
        }
        var statusformData={
            checkedIds:checkedIds,
            assetsManagement:assets
        }
        console.log(formData);
        httpService.postRequest('assets/changeAssetsStatus',statusformData).then(function(res){
            var result=res;
            if(result.resultCode==0){
                var dealContent=getDealContent(formData.userInfo);
                angular.forEach(checkedIds,function(item,index){
                    var assetsOperation={
                        assetsId:item,
                        dealStatus:formData.operationData.dealStatus,
                        dealContent:dealContent,
                        remark:formData.operationData.remark
                    }
                    console.log(assetsOperation);
                    httpService.insertAssetsOperation(assetsOperation).then(function(res){
                        console.log(res);
                    });

                });
                $scope.moreData();

            }
            console.log(res);
        });
    }

    function getDealContent(userInfo){
        var dealContentArr=[];
        dealContentArr.push(userInfo.name);
        dealContentArr.push(userInfo.email);
        dealContentArr.push(userInfo.phone);
        return dealContentArr.join(",");
    }

    $scope.operationAssets=function(title,useStatus,dealStatus,useStatusFlag){
        var i = 0;
        var checkedItems=$scope.showData.filter(function(item,index){
            return item.checked;
        });

        if (checkedItems.length <= 0) {
            return;
        }

      /*  var formBoxData={
            title:title,
            useStatus:useStatus,
            dealStatus:dealStatus,
            checkNum:checkedItems.length,
            useStatusFlag:useStatusFlag
        };*/
       /* var modalInstance = $uibModal.open({
            templateUrl: 'html/systems/assets/receiveAssets.html',
            controller: 'receiveAssetsCtrl',
            backdrop: "static",
//			size: 'sm',
            resolve: {
                formBoxData: function () {
                    return formBoxData;
                }
            }
        });*/

            var checkedIds = checkedItems.map(function(item,index){
                return item.id;
            });
            console.log(checkedIds,formData);


    }


    $scope.receiveAssets = function () {
        var useStatusFlag='receive';
        //$scope.operationAssets(title,useStatus,dealStatus,useStatusFlag);
        $scope.formData.operationData.useStatus='领用';
        $scope.formData.operationData.useProcessStatus=1;
        $scope.formData.operationData.dealStatus='领用';
        $scope.changeUseStatus($rootScope.select_list_index,$scope.formData);
    };

    $scope.borrowAssets = function () {
        var useStatusFlag='borrow';
        $scope.formData.operationData.useStatus='借用';
        $scope.formData.operationData.useProcessStatus=2;
        $scope.formData.operationData.dealStatus='借用';
        $scope.changeUseStatus($rootScope.select_list_index,$scope.formData);
    };

    $scope.backAssets = function () {
        var useStatusFlag='back';
        $scope.formData.operationData.useStatus='空闲';
        $scope.formData.operationData.useProcessStatus=0;
        $scope.formData.operationData.dealStatus='退回';
        $scope.changeUseStatus($rootScope.select_list_index,$scope.formData);
    };
    $scope.destroyAssets = function () {
        $scope.formData.operationData.useStatus='报废';
        $scope.formData.operationData.useProcessStatus=-1;
        $scope.formData.operationData.dealStatus='报废';
        $scope.changeUseStatus($rootScope.select_list_index,$scope.formData);
    };



    $scope.ok = function () {
        debugger;
    	switch ($scope.useStatusFlag){
            case 'receive':
                $scope.receiveAssets();
                break;
            case 'borrow':
                $scope.borrowAssets();
                break;
            case 'back':
                $scope.backAssets();
                break;
            case 'destroy':
                $scope.destroyAssets();
                break;
        }

    };

    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
});